﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.Custom;
using DWF.Studio.Host;
using DWF.Studio.ISerivces;
using DWF.Studio.Models;
using DWF.Studio.Project;
using DWF.Studio.RemoteCtrls;
using DWF.Studio.Script;
using DWF.Studio.Services;
using System;
using System.Activities.Expressions;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace DWF.Studio.Views
{

    /// <summary>
    /// UCtrlMainRegion.xaml 的交互逻辑
    /// </summary>
    public partial class PageMainRegion : Page
    {
        private ERegionLeftBarStatus _currentLeftBarStatus;
        private double _lastLeftBarWidth = 300;
        private double _lastRightBarWidth = 300;

        private ISearchService _searchService = null;

        private ProjectManager _projectManager = null;

        private ObservableCollection<Models.SearchActivityModel> _obsSelectResult = new ObservableCollection<Models.SearchActivityModel>();

        public PageMainRegion()
        {
            InitializeComponent();

            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                init();
            }
        }

        private void init()
        {
            _currentLeftBarStatus = ERegionLeftBarStatus.ActivitiesToolBoxShow;

            GlobalActionHandler.ProjectOpened += openedProject;
            GlobalActionHandler.ProjectClosing += closeProject;
            GlobalActionHandler.ScriptSelected += scriptSelected;

            GlobalActionHandler.SwitchProjectPanel+= openedProject;
            GlobalActionHandler.SwitchActivityPanel+= closeProject;
            GlobalActionHandler.SwitchSnippetPanel+= openSnippet;
            GlobalActionHandler.ChangePropertyRegionWidth+= changePropertyRegionPanel;

            GlobalActionHandler.ProjectOpened += loadSearchExplorer;
            GlobalActionHandler.ProjectClosing += closeSearchExplorer;

            GlobalActionHandler.PackageChanging += closeSearchExplorer;
            GlobalActionHandler.PackageChanged += loadSearchExplorer;

            GlobalActionHandler.FindSearchText += OpenSearchExplorer;

            _searchService = new SearchService();
            _projectManager = ProjectManager.Instance;
            _obsSelectResult.Clear();
        }

        private void showProjecExplorer()
        {
            if (_currentLeftBarStatus == ERegionLeftBarStatus.ProjectExplorerShow)//关闭
            {
                if (columnLeftBar.Width.Value > 2)
                {
                    _lastLeftBarWidth = columnLeftBar.Width.Value;
                    columnLeftBar.Width = new GridLength(2);
                    _currentLeftBarStatus = ERegionLeftBarStatus.ProjectExplorerHide;
                }
                else
                {
                    this.uCtrlProjectExplorer.Visibility = Visibility.Visible;
                    this.uCtrlActivityToolBox.Visibility = Visibility.Hidden;
                    this.uCtrlSnippetExplorer.Visibility = Visibility.Hidden;
                    columnLeftBar.Width = new GridLength(_lastLeftBarWidth);
                    _currentLeftBarStatus = ERegionLeftBarStatus.ProjectExplorerShow;
                }
            }
            else
            {
                this.uCtrlProjectExplorer.Visibility = Visibility.Visible;
                this.uCtrlActivityToolBox.Visibility = Visibility.Hidden;
                this.uCtrlSnippetExplorer.Visibility = Visibility.Hidden;
                columnLeftBar.Width = new GridLength(_lastLeftBarWidth);
                _currentLeftBarStatus = ERegionLeftBarStatus.ProjectExplorerShow;
            }
        }

        private void showActiviesToolBox()
        {
            if (_currentLeftBarStatus == ERegionLeftBarStatus.ActivitiesToolBoxShow)//关闭
            {
                if (columnLeftBar.Width.Value > 2)
                {
                    _lastLeftBarWidth = columnLeftBar.Width.Value;
                    columnLeftBar.Width = new GridLength(2);
                    _currentLeftBarStatus = ERegionLeftBarStatus.ActivitiesToolBoxHide;
                }
                else
                {
                    this.uCtrlProjectExplorer.Visibility = Visibility.Hidden;
                    this.uCtrlActivityToolBox.Visibility = Visibility.Visible;
                    this.uCtrlSnippetExplorer.Visibility = Visibility.Hidden;
                    this.uCtrlActivityToolBox.Refresh();
                    columnLeftBar.Width = new GridLength(_lastLeftBarWidth);
                    _currentLeftBarStatus = ERegionLeftBarStatus.ActivitiesToolBoxShow;
                }
            }
            else
            {
                this.uCtrlProjectExplorer.Visibility = Visibility.Hidden;
                this.uCtrlActivityToolBox.Visibility = Visibility.Visible;
                this.uCtrlSnippetExplorer.Visibility = Visibility.Hidden;
                this.uCtrlActivityToolBox.Refresh();
                columnLeftBar.Width = new GridLength(_lastLeftBarWidth);
                _currentLeftBarStatus = ERegionLeftBarStatus.ActivitiesToolBoxShow;
            }
        }
        private void showSnippetExplorer()
        {
            if (_currentLeftBarStatus == ERegionLeftBarStatus.SnippetExplorerShow)//关闭
            {
                if (columnLeftBar.Width.Value > 2)
                {
                    _lastLeftBarWidth = columnLeftBar.Width.Value;
                    columnLeftBar.Width = new GridLength(2);
                    _currentLeftBarStatus = ERegionLeftBarStatus.SnippetExplorerHide;
                }
                else
                {
                    this.uCtrlProjectExplorer.Visibility = Visibility.Hidden;
                    this.uCtrlActivityToolBox.Visibility = Visibility.Hidden;
                    this.uCtrlSnippetExplorer.Visibility = Visibility.Visible;
                    this.uCtrlSnippetExplorer.Refresh();
                    columnLeftBar.Width = new GridLength(_lastLeftBarWidth);
                    _currentLeftBarStatus = ERegionLeftBarStatus.SnippetExplorerShow;
                }
            }
            else
            {
                this.uCtrlProjectExplorer.Visibility = Visibility.Hidden;
                this.uCtrlActivityToolBox.Visibility = Visibility.Hidden;
                this.uCtrlSnippetExplorer.Visibility = Visibility.Visible;
                this.uCtrlSnippetExplorer.Refresh();
                columnLeftBar.Width = new GridLength(_lastLeftBarWidth);
                _currentLeftBarStatus = ERegionLeftBarStatus.SnippetExplorerShow;
            }
        }
        private void showPropertyBar(EventArgs e)
        {
            if (this.columnRightBar.Width.Value == 0)
            {
                columnRightBar.Width = new GridLength(_lastRightBarWidth);
            }
        }

        private void scriptSelected(TagEventArgs<ScriptBase> e)
        {
            var script = e.Tag;
            if (script != null)
            {
                var ext = System.IO.Path.GetExtension(script.FileName);
                if (ext == StaticResource.XamlExtension)
                {
                    if (this.columnRightBar.Width.Value == 0)
                    {
                        columnRightBar.Width = new GridLength(_lastRightBarWidth);
                    }
                }
                else
                {
                    if (this.columnRightBar.Width.Value > 0)
                    {
                        _lastRightBarWidth = columnRightBar.Width.Value;
                        columnRightBar.Width = new GridLength(0);
                    }
                }
            }
            else
            {
                if (this.columnRightBar.Width.Value > 0)
                {
                    _lastRightBarWidth = columnRightBar.Width.Value;
                    columnRightBar.Width = new GridLength(0);
                }
            }
        }

        private void openedProject(EventArgs e)
        {
            btnProject.IsChecked = true;
            showProjecExplorer();
        }

        private void closeProject(EventArgs e)
        {
            btnActivies.IsChecked = true;
            showActiviesToolBox();
        }
        private void openSnippet(EventArgs e)
        {
            btnSnippets.IsChecked = true;
            showSnippetExplorer();
        }
        private void changePropertyRegionPanel(TagEventArgs<bool> e)
        {
            _lastRightBarWidth = columnRightBar.Width.Value;
            if (e.Tag)
            {
                if (_lastRightBarWidth <= 2)
                {
                    columnRightBar.Width = new GridLength(350);
                    _lastRightBarWidth = 350;
                }
                else
                {
                    columnRightBar.Width = new GridLength(2);
                    _lastRightBarWidth = 2;
                }
            }
            else
            {
                columnRightBar.Width = new GridLength(350);
                _lastRightBarWidth = 350;
            }
        }
        private void GridSplitter_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
        {
            _lastLeftBarWidth = columnLeftBar.Width.Value;

            if (_currentLeftBarStatus == ERegionLeftBarStatus.ActivitiesToolBoxShow)
            {
                if (_lastLeftBarWidth < 100)
                {
                    columnLeftBar.Width = new GridLength(2);
                    _lastLeftBarWidth = 350;
                    _currentLeftBarStatus = ERegionLeftBarStatus.ActivitiesToolBoxHide;
                }
            }
            else if (_currentLeftBarStatus == ERegionLeftBarStatus.ProjectExplorerShow)
            {
                if (_lastLeftBarWidth < 100)
                {
                    columnLeftBar.Width = new GridLength(2);
                    _lastLeftBarWidth = 350;
                    _currentLeftBarStatus = ERegionLeftBarStatus.ProjectExplorerHide;
                }
            }
            else if (_currentLeftBarStatus == ERegionLeftBarStatus.ActivitiesToolBoxHide)
            {
                if (_lastLeftBarWidth > 2)
                {
                    _currentLeftBarStatus = ERegionLeftBarStatus.ActivitiesToolBoxShow;
                }
            }
            else if (_currentLeftBarStatus == ERegionLeftBarStatus.ProjectExplorerHide)
            {
                if (_lastLeftBarWidth > 2)
                {
                    _currentLeftBarStatus = ERegionLeftBarStatus.ProjectExplorerShow;
                }
            }
        }

        /// <summary>
        /// 显示工程浏览器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnProject_Click(object sender, RoutedEventArgs e)
        {
            showProjecExplorer();
        }

        /// <summary>
        /// 显示宏工具栏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnActivies_Click(object sender, RoutedEventArgs e)
        {
            showActiviesToolBox();
        }

        /// <summary>
        /// 显示片段栏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnSnippets_Click(object sender, RoutedEventArgs e)
        {
            showSnippetExplorer();
        }

        #region 查询
        // <summary>
        /// 加载查询
        /// </summary>
        /// <param name="e"></param>
        private void loadSearchExplorer(EventArgs e)
        {
            searchBorder.Child = _searchService?.LoadSearchHost();
        }
        // <summary>
        /// 加载查询
        /// </summary>
        /// <param name="e"></param>
        private void OpenSearchExplorer(EventArgs e)
        {
            _searchService?.OpenSearchBox(this.ActualWidth);
        }
        
        private void closeSearchExplorer(EventArgs e)
        {
            _searchService?.Dispose();
        }
        #endregion

        private void btnBackStartPage_Click(object sender, RoutedEventArgs e)
        {
            GlobalActionHandler.BackStartPage?.Invoke(new TagEventArgs<bool>(true));
        }
    }
}
